package com.example.administrator.yuejian35.Club.activity;

import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.transition.Explode;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.example.administrator.yuejian35.Club.adapter.VideoListAdapter;
import com.example.administrator.yuejian35.Club.bean.Video;
import com.example.administrator.yuejian35.Club.model.ClubModel;
import com.example.administrator.yuejian35.Home.SampleListener;
import com.example.administrator.yuejian35.Home.activity.MasterActivity;
import com.example.administrator.yuejian35.Home.adapter.RecyclerItemViewHolder;
import com.example.administrator.yuejian35.R;
import com.shuyu.gsyvideoplayer.utils.CommonUtil;
import com.shuyu.gsyvideoplayer.utils.Debuger;
import com.shuyu.gsyvideoplayer.utils.ListVideoUtil;
import com.shuyu.gsyvideoplayer.video.base.GSYVideoPlayer;

import java.util.List;

import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class VideoListActivity extends AppCompatActivity {
    private RecyclerView video_rv;
    private List<Video> list;
    private ImageView ic_back;

    private FrameLayout videoFullContainer;

    private LinearLayoutManager linearLayoutManager;

    private VideoListAdapter recyclerBaseAdapter;

    //private List<Course> dataList = new ArrayList<>();

    private ListVideoUtil listVideoUtil;
    private int lastVisibleItem;
    private int firstVisibleItem;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
            getWindow().setEnterTransition(new Explode());
            getWindow().setExitTransition(new Explode());
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_list);
        video_rv= (RecyclerView) findViewById(R.id.video_rv);
        ic_back= (ImageView) findViewById(R.id.video_pic);
        videoFullContainer = (FrameLayout) findViewById(R.id.video1_full_container);

        ButterKnife.bind(this);

        InternetCourse();


        video_rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                firstVisibleItem   = linearLayoutManager.findFirstVisibleItemPosition();
                lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
                Debuger.printfLog("firstVisibleItem " + firstVisibleItem +" lastVisibleItem " + lastVisibleItem);
                //大于0说明有播放,//对应的播放列表TAG
                if (listVideoUtil.getPlayPosition() >= 0 && listVideoUtil.getPlayTAG().equals(RecyclerItemViewHolder.TAG)) {
                    //当前播放的位置
                    int position = listVideoUtil.getPlayPosition();
                    //不可视的是时候
                    if ((position < firstVisibleItem || position > lastVisibleItem)) {
                        //如果是小窗口就不需要处理
                        if (!listVideoUtil.isSmall() && !listVideoUtil.isFull()) {
                            //小窗口
                            int size = CommonUtil.dip2px(VideoListActivity.this, 150);
                            //actionbar为true才不会掉下面去
                            listVideoUtil.showSmallVideo(new Point(size, size), true, true);
                        }
                    } else {
                        if (listVideoUtil.isSmall()) {
                            listVideoUtil.smallVideoToNormal();
                        }
                    }
                }
            }
        });


        ic_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

    private void initVideoUtil(){
        listVideoUtil = new ListVideoUtil(VideoListActivity.this);
        listVideoUtil.setFullViewContainer(videoFullContainer);
        listVideoUtil.setHideStatusBar(true);
        //小窗口关闭被点击的时候回调处理回复页面
        listVideoUtil.setVideoAllCallBack(new SampleListener() {
            @Override
            public void onPrepared(String url, Object... objects) {
                super.onPrepared(url, objects);
                Debuger.printfLog("Duration " + listVideoUtil.getDuration() + " CurrentPosition " +
                        listVideoUtil.getCurrentPositionWhenPlaying());
            }

            @Override
            public void onQuitSmallWidget(String url, Object... objects) {
                super.onQuitSmallWidget(url, objects);
                //大于0说明有播放,//对应的播放列表TAG
                if (listVideoUtil.getPlayPosition() >= 0 && listVideoUtil.getPlayTAG().equals(RecyclerItemViewHolder.TAG)) {
                    //当前播放的位置
                    int position = listVideoUtil.getPlayPosition();
                    //不可视的是时候
                    if ((position < firstVisibleItem || position > lastVisibleItem)) {
                        //释放掉视频
                        listVideoUtil.releaseVideoPlayer();
                        recyclerBaseAdapter.notifyDataSetChanged();
                    }
                }

            }
        });

    }


    private void InternetCourse() {
        ClubModel clubmodel = new ClubModel();
        final String clubId = getIntent().getStringExtra("clubid");
        Call call = clubmodel.getvideobyclub(clubId);
        call.enqueue(new Callback<List<Video>>() {
            @Override
            public void onResponse(Call<List<Video>> call, Response<List<Video>> response) {

                if(VideoListActivity.this!=null) {
                    list = response.body();

                     recyclerBaseAdapter = new VideoListAdapter(VideoListActivity.this, list);
                    video_rv.setAdapter(recyclerBaseAdapter);

                    linearLayoutManager = new LinearLayoutManager(VideoListActivity.this);
                    video_rv.setLayoutManager(linearLayoutManager);

                    initVideoUtil();
                    recyclerBaseAdapter.setListVideoUtil(listVideoUtil);



                    /*linearLayoutManager = new LinearLayoutManager(MasterActivity.this);
                    rv.setLayoutManager(linearLayoutManager);

                    recyclerBaseAdapter = new RecyclerBaseAdapter(MasterActivity.this, courselist);
                    rv.setAdapter(recyclerBaseAdapter);
                    initVideoUtil();
                    recyclerBaseAdapter.setListVideoUtil(listVideoUtil);*/

                }
            }

            @Override
            public void onFailure(Call<List<Video>> call, Throwable t) {

            }
        });


    }


    @Override
    public void onBackPressed() {
        if (listVideoUtil.backFromFull()) {
            return;
        }
        super.onBackPressed();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        listVideoUtil.releaseVideoPlayer();
        GSYVideoPlayer.releaseAllVideos();
    }


}


